clear

********************************
*First: 1982 survey
*********************************

use 08328-0001-Data.dta
save 29aug12NSFG82resp.dta, replace

*Note that SRRACE is never missing, the omitted category is other
gen black = SRRACE==1
gen white = SRRACE==2

egen totalobs = count(ID_NUM)
egen totalwt = total(FINALWT)
gen wtuse = FINALWT*(totalobs/totalwt)
egen wtcheck = total(wtuse)

*NOTE that FMARITAL is never missing and there is no value of 2--value of 3 is widowed
gen married = FMARITAL==1
gen divsep = (FMARITAL==4|FMARITAL==5)
gen nevermarried = FMARITAL==6

*Note that SPANISH2 is never missing, the omitted category is non-hispanic
gen hispanic = SPANISH2==1

*Note that religion is never missing, the omitted categories are None, Jewish, Other
gen protestant = RELIGION==1
gen catholic = RELIGION==2

*Note that LABORFOR is never missing
*working is zero for  unemp, in school, keeping house, other.
*working is one for full-time, part-time, with job not at work, and maternity leave

gen working = (LABORFOR==1|LABORFOR==2|LABORFOR==3|LABORFOR==5)

gen childreneverborn = PARITY

*For working mother use the variable that gives mother's occupation--F9
*Note that values greater than 889 are 992-999
*Note that 992 is CETA (a jobs training program)--but no one has that value
*994 is military, but no one has that value, 993,996 are did not work
*995 is did not live with mother--code working mother as missing in that
*case.

gen workingmother = (F9>=3 & F9<=889)
replace workingmother = . if (F9==997|F9==998|F9==999|F9==995)

gen familyincome = .
replace familyincome = 1250 if F70==1
replace familyincome = 3750 if F70==2
replace familyincome = 5500 if F70==3
replace familyincome = 6500 if F70==4
replace familyincome = 7500 if F70==5
replace familyincome = 8500 if F70==6
replace familyincome = 9500 if F70==7
replace familyincome = 10500 if F70==8
replace familyincome = 11500 if F70==9
replace familyincome = 12500 if F70==10
replace familyincome = 13999 if F70==11
replace familyincome = 15999 if F70==12
replace familyincome = 18499 if F70==13
replace familyincome = 22499 if F70==14
replace familyincome = 29999 if F70==15
replace familyincome = 42499 if F70==16
replace familyincome = 74499 if F70==17
replace familyincome = . if (F70==97|F70==98|F70==99)

save, replace

clear

use 08328-0002-Data.dta
save 29aug12NSFG82interv.dta, replace

*Note that age at pregnancy outcome is 4 decimal places, with an implied decimal
*point after the first two
	*So 1608 is 16.08 years, which is 16 years and 1 month


*Merge interval file with the respondent file

save, replace

merge m:1 ID_NUM using 29aug12NSFG82resp.dta
rename _merge merge1982interval

save NSFG1982merged30aug12.dta, replace

gen survey = 1982

gen agefirstconception = AGECON1
replace agefirstconception = . if AGECON1==9999

*Getting date of conception (see page 424 of codebook)
gen f= .
replace f= DATE1CON/12
tostring f, gen(fstr) force
split fstr, p(.)
gen secondf = substr(fstr, 3, 8)
destring secondf, gen(secondfnum)
gen monthf = secondfnum*12
gen monthfUSE = round(monthf)
destring fstr1, gen(fnum1)
gen yearfUSE = fnum1+1900

*For December, make month = 11.8, so then
*will be in the correct year (i.e. month1concept/12 will not equal 1 and push
*the year forward).

replace monthfUSE = 11.8 if fstr2=="" & fstr~="" & fstr~="."
replace yearfUSE = yearfUSE-1 if fstr2=="" & fstr~="" & fstr~="."

*Now fix with missing months
gen h = DATE1CON if DATE1CON>=9622 & DATE1CON<=9940
tostring h, gen(hstr) force
gen firsth = substr(hstr,2,3)
destring firsth, gen(firsthnum)
gen h2 = firsthnum/12
tostring h2, gen(h2str) force
split h2str, p(.)
destring h2str1, gen(h2num1)
replace yearfUSE=h2num1+1900 if DATE1CON>=9622 & DATE1CON<=9940
gen h2num2 = substr(h2str,3,8)
destring h2num2, gen(h2num2num)
gen monthfmiss = h2num2num*12 if DATE1CON>=9622 & DATE1CON<=9940
replace monthfUSE = round(monthfmiss) if DATE1CON>=9622 & DATE1CON<=9940
replace monthfUSE = 11.8 if h2num2=="" & h~=. & DATE1CON>=9622 & DATE1CON<=9940
replace yearfUSE = yearfUSE-1 if h2num2=="" & h~=. & DATE1CON>=9622 & DATE1CON<=9940

replace yearfUSE = . if DATE1CON==9999
replace monthfUSE = . if DATE1CON==9999

rename yearfUSE year1concept
rename monthfUSE month1concept

gen date1concept = year1concept+(month1concept/12)

*Count the number of the respondent's own children living with her

gen child1inhh = (CHHE1==1)
bysort ID_NUM: egen totchild1inhh = total(child1inhh)

gen child2inhh = (CHHE2==1)
bysort ID_NUM: egen totchild2inhh = total(child2inhh)

gen child3inhh = (CHHE3==1)
bysort ID_NUM: egen totchild3inhh = total(child3inhh)

gen totchildinhh = totchild1inhh + totchild2inhh + totchild3inhh

gen CASEID = ID_NUM
save, replace
erase 29aug12NSFG82resp.dta
erase 29aug12NSFG82interv.dta
********************************************
*Now do 1988
********************************************

clear
clear matrix

use 09473-0001-Data.dta
save 7sept12NSFG88resp.dta, replace

egen totalobs = count(CASEID)
egen totalwt = total(W5)
gen wtuse = W5*(totalobs/totalwt)
egen wtcheck = total(wtuse)

*Note that RACE is never missing. The left out category is Other

gen black = RACE==1
gen white = RACE==2

*Note that FMARITAL is never missing and there is never a value of 2, 3 is widowed (page 620 of codebook)
gen married = FMARITAL==1
gen divsep = (FMARITAL==4|FMARITAL==5)
gen nevermarried = FMARITAL==6

*HISPANIC is never missing
gen hispanic = HISPANIC==1

*RELIGION is never missing, omitted categories are None, Jewish, Other.
gen protestant = RELIGION==1
gen catholic = RELIGION==2

*Note that LABORFOR is never missing
*working is one for full-time, part-time, two part-time jobs, working but on vacation, strike or 
*temp illness, working but on maternity leave.
*working is zero for unemp, in school, keeping house, other
gen working = (LABORFOR==1|LABORFOR==2|LABORFOR==3|LABORFOR==4|LABORFOR==9)

gen childreneverborn = PARITY

*Note that this is a different question than in 1982--it is whether the respondent's mother
*worked when she was growing up.  The value 3 is for no.
gen workingmother = (A_30<=2)
replace workingmother = . if (A_30==7|A_30==8|A_30==9)

gen familyincome = .
replace familyincome = 1250 if F_93==1
replace familyincome = 3750 if F_93==2
replace familyincome = 5500 if F_93==3
replace familyincome = 6500 if F_93==4
replace familyincome = 7500 if F_93==5
replace familyincome = 8500 if F_93==6
replace familyincome = 9500 if F_93==7
replace familyincome = 10500 if F_93==8
replace familyincome = 11500 if F_93==9
replace familyincome = 12500 if F_93==10
replace familyincome = 13999 if F_93==11
replace familyincome = 15999 if F_93==12
replace familyincome = 18499 if F_93==13
replace familyincome = 22499 if F_93==14
replace familyincome = 29999 if F_93==15
replace familyincome = 42499 if F_93==16
replace familyincome = 74499 if F_93==17
replace familyincome = . if (F_93==97|F_93==98|F_93==99)

save, replace

clear

use 09473-0002-Data.dta
save 7sept12NSFG88interv.dta, replace

rename SEQNUM CASEID

*merge this interval file with the respondent file

save, replace

merge m:1 CASEID using 7sept12NSFG88resp.dta
rename _merge merge1988interval

save NSFG1988merged7sept12.dta, replace

gen survey = 1988

gen agefirstconception = AGECON1
replace agefirstconception = . if AGECON1==9999

*Generate date of first conception (see page 578 of code book)
*Get month and year of first conception
gen f= .
replace f= DATE1CON/12
tostring f, gen(fstr) force
split fstr, p(.)
gen secondf = substr(fstr, 3, 8)
destring secondf, gen(secondfnum)
gen monthf = secondfnum*12
gen monthfUSE = round(monthf)
destring fstr1, gen(fnum1)
gen yearfUSE = fnum1+1900

replace monthfUSE = 11.8 if fstr2=="" & fstr~="" & fstr~="."
replace yearfUSE = yearfUSE-1 if fstr2=="" & fstr~="" & fstr~="."

*Now fix with missing months
gen h = DATE1CON if DATE1CON>=90683 & DATE1CON<=91064
tostring h, gen(hstr) force
gen firsth = substr(hstr,2,4)
destring firsth, gen(firsthnum)
gen h2 = firsthnum/12
tostring h2, gen(h2str) force
split h2str, p(.)
destring h2str1, gen(h2num1)
replace yearfUSE=h2num1+1900 if DATE1CON>=90683 & DATE1CON<=91064
gen h2num2 = substr(h2str,3,8)
destring h2num2, gen(h2num2num)
gen monthfmiss = h2num2num*12 if DATE1CON>=90683 & DATE1CON<=91064
replace monthfUSE = round(monthfmiss) if DATE1CON>=90683 & DATE1CON<=91064
replace monthfUSE = 11.8 if h2num2=="" & h~=. & DATE1CON>=90683 & DATE1CON<=91064
replace yearfUSE = yearfUSE-1 if h2num2=="" & h~=. & DATE1CON>=90683 & DATE1CON<=91064

rename yearfUSE year1concept
rename monthfUSE month1concept

gen date1concept = year1concept+(month1concept/12)

save, replace

*Count the number of the respondent's own children living with her

gen child1inhh = (BOX11_1==1)
bysort CASEID: egen totchild1inhh = total(child1inhh)

gen child2inhh = (BOX11_2==1)
bysort CASEID: egen totchild2inhh = total(child2inhh)

gen child3inhh = (BOX11_3==1)
bysort CASEID: egen totchild3inhh = total(child3inhh)

gen totchildinhh = totchild1inhh + totchild2inhh + totchild3inhh

save, replace
erase 7sept12NSFG88resp.dta
erase 7sept12NSFG88interv.dta
***************************************
*Now do 1995
***************************************

clear
clear matrix

use 06960-0001-Data.dta
save 7sept12NSFG95resp.dta, replace

egen totalobs = count(CASEID)
egen totalwt = total(POST_WT)
gen wtuse = POST_WT*(totalobs/totalwt)
egen wtcheck = total(wtuse)

*Note that RACE is never missing. Omitted category is Other
gen black = RACE==1
gen white = RACE==2

*NOTE that FMARITAL is never missing and never takes on a value of 2, 3 is widowed--coding is the same for all
*years (page 10 of codebook)
gen married = FMARITAL==1
gen divsep = (FMARITAL==4|FMARITAL==5)
gen nevermarried = FMARITAL==6

*Note that HISPANIC is never missing--omitted category is non-hispanic
gen hispanic = HISPANIC==1

*Note that religion is never missing--omitted categories are None, no specific denomination, Jewish,
*other
gen protestant = RELIGION==1
gen catholic = RELIGION==2

*working is one for full-time, part-time, working but on vacation, strike or 
*temp illness, working but on maternity leave, 2 part-time jobs, 
*working is zero for unemp, in school, keeping house,
*permanent disability, doing nothing, other.
*LABORFOR is never missing.

gen working = (LABORFOR==1|LABORFOR==2|LABORFOR==3|LABORFOR==4|LABORFOR==8)

gen childreneverborn = PARITY

*Question is whether mother worked during childhood, set to missing if no mother figure identified
gen workingmother = (MOMWORK==1|MOMWORK==2|MOMWORK==3)
replace workingmother = . if MOMWORK==95

gen familyincome = .
replace familyincome = 3500 if TOTINC==1
replace familyincome = 7750 if TOTINC==2
replace familyincome = 9250 if TOTINC==3
replace familyincome = 10999 if TOTINC==4
replace familyincome = 12999 if TOTINC==5
replace familyincome = 14999 if TOTINC==6
replace familyincome = 16999 if TOTINC==7
replace familyincome = 18999 if TOTINC==8
replace familyincome = 22499 if TOTINC==9
replace familyincome = 27499 if TOTINC==10
replace familyincome = 34499 if TOTINC==11
replace familyincome = 44499 if TOTINC==12
replace familyincome = 54499 if TOTINC==13
replace familyincome = 64499 if TOTINC==14
replace familyincome = 74499 if TOTINC==15
replace familyincome = 84499 if TOTINC==16
replace familyincome = 94499 if TOTINC==17
replace familyincome = 124499 if TOTINC==18
replace familyincome = . if (TOTINC==97|TOTINC==98|TOTINC==99)

save, replace

clear

use 06960-0002-Data.dta
save 7sept12NSFG95interv.dta, replace

*merge this interval file with the respondent file

save, replace

merge m:1 CASEID using 7sept12NSFG95resp.dta
rename _merge merge1995interval

save NSFG1995merged7sept12.dta, replace

gen survey = 1995

gen agefirstconception = AGECON01

*Get month and year of first conception
gen f= .
replace f= DATCON01/12
tostring f, gen(fstr) force
split fstr, p(.)
gen secondf = substr(fstr, 3, 8)
destring secondf, gen(secondfnum)
gen monthf = secondfnum*12
gen monthfUSE = round(monthf)
destring fstr1, gen(fnum1)
gen yearfUSE = fnum1+1900
replace monthfUSE = 11.8 if fstr2=="" & fstr~="" & fstr~="."
replace yearfUSE = yearfUSE-1 if fstr2=="" & fstr~="" & fstr~="."

rename yearfUSE year1concept
rename monthfUSE month1concept

*Note there are no codes for 1995 for people remembering the year not the month.

gen date1concept = year1concept+(month1concept/12)

*Count the number of the respondent's own children living with her

gen child1inhh = (LIVEHER1==1)
bysort CASEID: egen totchild1inhh = total(child1inhh)

gen child2inhh = (LIVEHER2==1)
bysort CASEID: egen totchild2inhh = total(child2inhh)

gen totchildinhh = totchild1inhh + totchild2inhh


save, replace
erase 7sept12NSFG95resp.dta
erase 7sept12NSFG95interv.dta
*****************************************
*Now do 1973
******************************************

clear

use 07898-0001-Data.dta
save 17feb14NSFG73resp.dta, replace

*Note that SRRACE is never missing, the omitted category is other
gen black = RACE==1
gen white = RACE==2

egen totalobs = count(INT_ID)
egen totalwt = total(PSWT)
gen wtuse = PSWT*(totalobs/totalwt)
egen wtcheck = total(wtuse)

*NOTE that MAR_STAT is never missing--only have nevermarried if they are single with children of their own 
*in the household. So the choices are married, informal, widowed, divorced/annulled, separated, single with
*own children--if single with own children must be never married--because not divorced or widowed.
gen married = MAR_STAT==1
gen divsep = (MAR_STAT==4|MAR_STAT==5)
gen nevermarried = MAR_STAT==6

*Note that ETH_SPANISH1 is never missing, the omitted category is not of Spanish origin or descent
gen hispanic = ETH_SPANISH1==1

*Note that religion is never missing.  Here they break down protestant into different 
*denominations...so group them all as protestant. Omitted categories are 
	*1. Jewish
	*2. Other, unspecified
	*3. Eastern Orthodox
	*4. Non-traditional & semi-Christian
	*5. Non-Christian
	*6. None, atheist, agnostic
	*7. Mormon & Christian Scientist

gen protestant = REL_DENOMINA>=20 & REL_DENOMINA<=28

*Note that Catholic here is Roman Catholic or Old Catholic/Polish National
gen catholic = (REL_DENOMINA==10|REL_DENOMINA==42)

*working is zero for unemp, retired, in school, keeping house.
*working is one for full-time, part-time, with job not at work, and with job on
*maternity leave

gen working = (EMP_LASTWEEK==1|EMP_LASTWEEK==2|EMP_LASTWEEK==3|EMP_LASTWEEK==4)

*If don't give non-grouped income, then give grouped income
*this variable is non-missing for everyone--so if give non-grouped data
*becomes grouped in this variable.

gen familyincome = .
replace familyincome = 500 if FINC_TOTALCA==1
replace familyincome = 1500 if FINC_TOTALCA==2
replace familyincome = 2500 if FINC_TOTALCA==3
replace familyincome = 3500 if FINC_TOTALCA==4
replace familyincome = 4500 if FINC_TOTALCA==5
replace familyincome = 5500 if FINC_TOTALCA==6
replace familyincome = 6500 if FINC_TOTALCA==7
replace familyincome = 7500 if FINC_TOTALCA==8
replace familyincome = 8500 if FINC_TOTALCA==9
replace familyincome = 9500 if FINC_TOTALCA==10
replace familyincome = 11000 if FINC_TOTALCA==11
replace familyincome = 13500 if FINC_TOTALCA==12
replace familyincome = 20000 if FINC_TOTALCA==13
replace familyincome = 30000 if FINC_TOTALCA==14

save, replace

clear

use 07898-0002-Data.dta
save 17feb14NSFG73interv.dta, replace


*merge this interval file with the respondent file
	*Note that the interval file is not in long form, but in wide form
	
save, replace

merge 1:1 INT_ID using 17feb14NSFG73resp.dta
rename _merge merge1973interval

save NSFG1973merged17feb14.dta, replace

gen survey = 1973

*To get age at first conception (in months) find out the month/year pregnancy ended. Then subtract the
*respondent's birth month and year.

*First get the month of conception for the first pregnancy (in century months)
	*Note that the month in which the pregnancy ended is never unknown.
	
gen date1conceptcm = PREG1_DTENDED - 9 if (PREG1_OUTCOME==3|PREG1_OUTCOME==4)
#delimit ;
replace date1conceptcm = PREG1_DTENDED-PREG1_NUMMMP if (PREG1_OUTCOME==5|
PREG1_OUTCOME==6);
#delimit cr

*Now make a date of conception variable in years.

gen f= .
replace f= date1conceptcm/12
tostring f, gen(fstr) force
split fstr, p(.)
gen secondf = substr(fstr, 3, 8)
destring secondf, gen(secondfnum)
gen monthf = secondfnum*12
gen monthfUSE = round(monthf)
destring fstr1, gen(fnum1)
gen yearfUSE = fnum1+1900
replace monthfUSE = 11.8 if fstr2=="" & fstr~="" & fstr~="."
replace yearfUSE = yearfUSE-1 if fstr2=="" & fstr~="" & fstr~="."

rename yearfUSE year1concept
rename monthfUSE month1concept

gen date1concept = year1concept+(month1concept/12)

*Now subtract the birth month/year (in century months) of the respondent.  Note that the
*respondent's month of 
*birth is never missing...so don't need to worry about setting it equal to June.

gen agefirstconceptionmonths = date1conceptcm-DOB_MMYY

*Now make the age in years
gen agefirstconception = (agefirstconceptionmonths/12)*100

gen CASEID = INT_ID
save, replace

erase 17feb14NSFG73resp.dta
erase 17feb14NSFG73interv.dta
*****************************************
*Now do 1976
******************************************

clear

use 07902-0001-Data.dta
save 19feb14resp1976.dta, replace

*Note that F1 is never missing, the omitted category is other
gen black = F1==1
gen white = F1==2

egen totalobs = count(RESPID)
egen totalwt = total(WTFINAL)
gen wtuse = WTFINAL*(totalobs/totalwt)
egen wtcheck = total(wtuse)

*NOTE that MARSTAT is never missing--only have nevermarried if they are single with children of their own 
*in the household. So the choices are married, informal, widowed/divorced/annulled/separated, single with
*own children--if single with own children must be never married--because not divorced or widowed.
gen married = MARSTAT==1
gen divsep = (MARSTAT==3)
gen nevermarried = MARSTAT==4

*Note that HISPANIC is missing for about 10 observations.  The zeros
*here are "not of Spanish origin or descent"
gen hispanic = HISPANIC==1
replace hispanic = . if HISPANIC==9

*Note that religion is sometimes missing (respondent's refuse
*or it is not ascertained).

gen protestant = F44==2
replace protestant = . if (F44==7|F44==9)

*Note that Catholic here is Roman Catholic or Old Catholic/Polish National
gen catholic = F44==1
replace catholic = . if (F44==7|F44==9)

*The labor force variable is 
*separate for those who are currently pregnant and those who are not currently
*pregnant.  The question is only asked for people who worked in the last 12 months
*working is zero for  unemp, in school, keeping house, other.
*working is one for full-time, part-time, with job not at work, and with job on
*maternity leave

gen working = 1 if (F26==1|F26==2|F26==3|F26==4) & F25~=0
replace working = 0 if (F26==5|F26==6|F26==7|F26==8|F25==0)
*Note that this working variable is always non-missing for the currently pregnant.

replace working = 1 if (F32==1|F32==2|F32==3) 
replace working = 0 if (F32==5|F32==6|F32==7|F32==8)
replace working = . if F32==99

#delimit cr
*Non-grouped family income in past twelve months--missing if people
*used flash cards
	*Max income is 99996.
gen familyincomenongrouped = F68
replace familyincomenongrouped = . if (F68==99998|F68==99999)

*If don't give non-grouped income, then give grouped income
*this variable is missing if gave non-grouped income.

gen familyincomegrouped = .
replace familyincomegrouped = 500 if (F69==1|F69==21)
replace familyincomegrouped = 1500 if (F69==2|F69==22)
replace familyincomegrouped = 2500 if (F69==3|F69==23)
replace familyincomegrouped = 3500 if (F69==4|F69==24)
replace familyincomegrouped = 4500 if (F69==5|F69==25)
replace familyincomegrouped = 5500 if (F69==6|F69==26)
replace familyincomegrouped = 6500 if (F69==7|F69==27)
replace familyincomegrouped = 7500 if (F69==8|F69==28)
replace familyincomegrouped = 8500 if (F69==9|F69==29)
replace familyincomegrouped = 9500 if (F69==10|F69==30)
replace familyincomegrouped = 11000 if (F69==11|F69==31)
replace familyincomegrouped = 13500 if (F69==12|F69==32)
replace familyincomegrouped = 17500 if (F69==13|F69==33)
replace familyincomegrouped = 22500 if (F69==14|F69==34)
replace familyincomegrouped = 27500 if (F69==15|F69==35)

gen familyincome = familyincomegrouped
#delimit ;
replace familyincome = 500 if F68<=999 & familyincomenongrouped~=.;
replace familyincome = 1500 if F68>=1000 
& F68<=1999 & familyincomenongrouped~=.;
replace familyincome = 2500 if F68>=2000 
& F68<=2999 & familyincomenongrouped~=.;
replace familyincome = 3500 if F68>=3000 
& F68<=3999 & familyincomenongrouped~=.;
replace familyincome = 4500 if F68>=4000 
& F68<=4999 & familyincomenongrouped~=.;
replace familyincome = 5500 if F68>=5000 
& F68<=5999 & familyincomenongrouped~=.;
replace familyincome = 6500 if F68>=6000 
& F68<=6999 & familyincomenongrouped~=.;
replace familyincome = 7500 if F68>=7000 
& F68<=7999 & familyincomenongrouped~=.;
replace familyincome = 8500 if F68>=8000 
& F68<=8999 & familyincomenongrouped~=.;
replace familyincome = 9500 if F68>=9000 
& F68<=9999 & familyincomenongrouped~=.;
replace familyincome = 11000 if F68>=10000 
& F68<=11999 & familyincomenongrouped~=.;
replace familyincome = 13500 if F68>=12000 
& F68<=14999 & familyincomenongrouped~=.;
replace familyincome = 17500 if F68>=15000 
& F68<=19999 & familyincomenongrouped~=.;
replace familyincome = 22500 if F68>=20000 
& F68<=24999 & familyincomenongrouped~=.;
replace familyincome = 27500 if F68>=25000 & F68<=99996
& familyincomenongrouped~=.;

#delimit cr
save, replace

clear

use 08181-0001-Data.dta
save 19feb14NSFG76interv.dta, replace

order ID, first
rename ID RESPID

*Merge this interval file with the respondent file
	
save, replace

merge m:1 RESPID using 19feb14resp1976.dta
rename _merge merge1976interval

save NSFG1976merged19feb14.dta, replace

gen survey = 1976

*To get age at first conception (in months) find out the month/year pregnancy ended. Then subtract the
*respondent's birth month and year.

*First get the month of conception for the first pregnancy (in century months)
	
gen pregnum1976 = BPREC1
gen monthspreg = BPREC5 
replace monthspreg = . if (BPREC5==98|BPREC5==99)	
#delimit ;	
gen date1conceptcm = BPREC2 - 9 if (BPREC4==1|BPREC4==3) & 
pregnum1976==1;
replace date1conceptcm = BPREC2-monthspreg if (BPREC4==2|BPREC4==4) & pregnum1976==1;
replace date1conceptcm = . if BPREC2==999;
#delimit cr

*Now fix with missing months for live birth--just take off the 9 and the remaining two digits are
*the year.  So if there was a live birth in 1946, and don't know month, assume
*that it is June, 1946. If it is a live birth assume that pregnancy duration was 9 months.

gen h = BPREC2 if BPREC2>=946 & BPREC2<=976
tostring h, gen(hstr) force
gen firsth = substr(hstr,2,2)
destring firsth, gen(firsthnum)

#delimit cr
*Fix missing months for pregnancy loss
*If there was not a live birth still assume it ended in June of the year given...
*so convert that to century months and subtract months pregnant

#delimit ;
gen cmmissingmonth = (firsthnum*12)+6 if pregnum1976==1 &
BPREC2>=946 & BPREC2<=976 & BPREC4<=4;
replace date1conceptcm = cmmissingmonth - monthspreg if pregnum1976==1 & 
(BPREC4==2|BPREC4==4) & BPREC2>=946 & BPREC2<=976;
replace date1conceptcm = cmmissingmonth - 9 if pregnum1976==1 & 
(BPREC4==1|BPREC4==3) & BPREC2>=946 & BPREC2<=976;

#delimit cr
*Now make a date of conception variable in years.  Note that this is only non-missing
*for the first pregnancy because date1conceptcm is only non-missing for the first
*pregnancy.

gen f= .
replace f= date1conceptcm/12
tostring f, gen(fstr) force
split fstr, p(.)
gen secondf = substr(fstr, 3, 8)
destring secondf, gen(secondfnum)
gen monthf = secondfnum*12
gen monthfUSE = round(monthf)
destring fstr1, gen(fnum1)
gen yearfUSE = fnum1+1900
replace monthfUSE = 11.8 if fstr2=="" & fstr~="" & fstr~="."
replace yearfUSE = yearfUSE-1 if fstr2=="" & fstr~="" & fstr~="."

rename yearfUSE year1concept
rename monthfUSE month1concept
gen date1concept = year1concept+(month1concept/12)
replace date1concept = . if (BPREC2==997|BPREC2==999)

*Now subtract the respondent's birth month/year (in century months).  Note that the month of 
*birth is missing for three individuals

*If the month of birth is missing assume it is June of that year.

gen respbirthdate = A7_CM if A7_CM<=900
gen hbirth = A7_CM if A7_CM>=901 & A7_CM<=998
tostring hbirth, gen(hstrbirth) force
gen firsthbirth = substr(hstrbirth,2,2)
destring firsthbirth, gen(firsthnumbirth)
replace respbirthdate = (firsthnumbirth*12)+6 if A7_CM>=901 & A7_CM<=998

gen agefirstconceptionmonths = date1conceptcm-respbirthdate

*Now make the age in years
gen agefirstconception = (agefirstconceptionmonths/12)*100

gen CASEID = RESPID
save, replace

erase 19feb14resp1976.dta
erase 19feb14NSFG76interv.dta

*Now append everything

clear
clear matrix

use NSFG1982merged30aug12.dta
append using NSFG1988merged7sept12.dta
append using NSFG1995merged7sept12.dta
append using NSFG1976merged19feb14.dta
append using NSFG1973merged17feb14.dta

gen agelt15 = agefirstconception<1500
replace agelt15 = . if agefirstconception==.

save ALLNSFG24feb14.dta, replace
erase NSFG1982merged30aug12.dta
erase NSFG1988merged7sept12.dta
erase NSFG1995merged7sept12.dta
erase NSFG1976merged19feb14.dta
erase NSFG1973merged17feb14.dta
